package com.patrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patrol.dao.TravelPathDao;
import com.patrol.pojo.TravelPath;
import com.patrol.service.TravelPathService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 房梦豪
 * @version 1.0
 * @description: TODO
 * @date 2021/8/12 16:54
 */
@Service
public class TravelPathServiceImpl implements TravelPathService {

    @Resource
    private TravelPathDao travelPathDao;

    @Override
    public TravelPath selectOneByTravel(Integer travel) {
        return travelPathDao.selectById(travel);
    }

    @Override
    public boolean insertOne(TravelPath travelPath) {
        return 1 == travelPathDao.insert(travelPath);
    }

    @Override
    public boolean updateOneById(TravelPath travelPath) {
        return 1 == travelPathDao.updateById(travelPath);
    }

    @Override
    public boolean deleteOneById(Integer id) {
        return 1 == travelPathDao.deleteById(id);
    }

    @Override
    public Page<TravelPath> selectAll(long page, long limit) {
        Page<TravelPath> travelPathPage = new Page<>(page, limit);
        return travelPathDao.selectPage(travelPathPage, null);
    }

    @Override
    public Map<Integer, List<Double>> selectUserAllLocation(Integer userId) {
        Map<Integer, List<Double>> map = new HashMap<>();
        QueryWrapper<TravelPath> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<TravelPath> travelPaths = travelPathDao.selectList(queryWrapper);
        iterator(map, travelPaths);
        return map;
    }

    @Override
    public Map<Integer, List<Double>> selectUserLocationByDate(Integer userId, String startDate, String endDate) {
        Map<Integer, List<Double>> map = new HashMap<>();
        List<TravelPath> travelPaths = travelPathDao.selectAllPathByDate(userId, startDate, endDate);
        iterator(map, travelPaths);
        return map;
    }

    private void iterator(Map<Integer, List<Double>> map, List<TravelPath> travelPaths) {
        for (TravelPath travelPath : travelPaths) {
            List<Double> list = new ArrayList<>(2);
            list.add(travelPath.getLatitudeX());
            list.add(travelPath.getLongitudeY());
            map.put(travelPath.getTaskId(), list);
        }
    }

}
